﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Configuration;

namespace DataService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }


        public Province InsertProvince(Province province)
        {
            using (AdventureEntities adventureEntities = new AdventureEntities())
            {
                adventureEntities.Provinces.Add(province);
                adventureEntities.SaveChanges();
            }
            return province;
        }

        public void DeleteProvince(Province province)
        {
            using (AdventureEntities adventureEntities = new AdventureEntities())
            {
                adventureEntities.Provinces.Attach(province);
                adventureEntities.Provinces.Remove(province);
                adventureEntities.SaveChanges();
            }
        }

        public Province GetProvince(Province province)
        {   
            var hydratedProvince = new Province();

            string test = ConfigurationManager.AppSettings[0];

            using (AdventureEntities adventureEntities = new AdventureEntities())
            {
                
                hydratedProvince = adventureEntities.Provinces.FirstOrDefault(x => x.ProvinceId == province.ProvinceIdImpl);
            }
            return hydratedProvince;
        }
    }
}
